package com.example.yingjiguanli_demo.config;

import com.example.yingjiguanli_demo.handler.GameWebSocketHandler;
import com.example.yingjiguanli_demo.interceptor.CustomHandshakeInterceptor;
import com.example.yingjiguanli_demo.service.IMatchesService;
import com.example.yingjiguanli_demo.utils.UserUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.WebSocketHandler;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final UserUtils userUtils;
    private final IMatchesService matchService;



    public WebSocketConfig(UserUtils userUtils, IMatchesService matchService) {
        this.userUtils = userUtils;
        this.matchService = matchService;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册现有的游戏 WebSocket 端点
        registry.addHandler(gameWebSocketHandler(), "/ws/game")
                .addInterceptors(new CustomHandshakeInterceptor(userUtils))  // 自定义握手拦截器
                .setAllowedOrigins("*");


    }

    // 现有的 WebSocket handler
    @Bean
    public WebSocketHandler gameWebSocketHandler() {
        return new GameWebSocketHandler(matchService);
    }

}


